package com.exsun.ex1077.ui.activity.launch

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
import com.exsun.ex1077.R
import com.exsun.ex1077.databinding.ActivityGuideBinding
import com.exsun.ex1077.databinding.ItemGuideBinding
import com.exsun.ex1077.ui.activity.login.LoginActivity
import com.exsun.ex1077.ui.activity.main.MainActivity
import com.zhangping.fastjetpack.base.BaseActivity
import com.zhangping.fastjetpack.base.BaseViewModel
import com.zhangping.fastjetpack.extend.putCache
import com.zhangping.fastjetpack.extend.toActivity
import com.zhangping.fastjetpack.extend.view.toBinding
import kotlinx.android.synthetic.main.activity_guide.*

/**
 * 描述：
 * 作者：ZhangPing
 * 创建时间：2023/3/20 13:36
 */
class GuideActivity: BaseActivity<BaseViewModel, ActivityGuideBinding>() {

    private lateinit var dotViews: List<View>

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        dotViews = arrayListOf(dot1, dot2, dot3)
        viewPager.offscreenPageLimit = 3
        viewPager.adapter = object : RecyclerView.Adapter<PageViewHolder>(){
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PageViewHolder {
                return PageViewHolder(LayoutInflater.from(this@GuideActivity).inflate(R.layout.item_guide, parent, false))
            }

            override fun onBindViewHolder(holder: PageViewHolder, position: Int) {
                val binding = holder.itemView.toBinding<ItemGuideBinding>()
                when (position) {
                    0 -> {
                        binding.icon.setImageResource(R.drawable.guide1)
                        binding.text.text = "位置感知，不只是车"
                    }
                    1 -> {
                        binding.icon.setImageResource(R.drawable.guide2)
                        binding.text.text = "支持扫码，快速绑定设备"
                    }
                    2 -> {
                        binding.icon.setImageResource(R.drawable.guide3)
                        binding.text.text = "里程报表分析，车队管理一手掌握"
                    }
                }
            }

            override fun getItemCount() = dotViews.size
        }
        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                updateStatus(position)
            }
        })
        next.setOnClickListener {
            val position = viewPager.currentItem
            if (position == dotViews.size - 1){
                "hasShowGuide".putCache(true)
                toActivity<LoginActivity>(finish = true)
            }else{
                viewPager.currentItem = position + 1
            }
        }
        skip.setOnClickListener {
            "hasShowGuide".putCache(true)
            toActivity<LoginActivity>(finish = true)
        }
    }

    class PageViewHolder(view: View): RecyclerView.ViewHolder(view)

    fun updateStatus(selectedPosition: Int){
        dotViews.forEachIndexed { index, view ->
            view.setBackgroundResource(if (index == selectedPosition) R.drawable.shape_radius99_bg_theme else R.drawable.shape_radius99_bg_d9d9d9)
        }
        next.text = if (selectedPosition == dotViews.size - 1) "立即体验" else "下一步"
    }
}